﻿using System;
using System.Diagnostics;
using System.IO;

namespace AutoTask
{
	public class WeeklyWord:Singleton<WeeklyWord>
	{
		/// <summary>
		/// 打开word
		/// </summary>
		/// <param name="fileName"></param>
		public void OpenWord(string fileName)
		{
			try
			{
				if (!File.Exists(fileName))
				{
					//自动创建一个word
					CopyWord("Weekly\\default.docx", fileName);
				}
				Process.Start(Path.GetFullPath(fileName));
			}
			catch (Exception e)
			{
				Console.WriteLine($"打开word出错:{e.ToString()}");
			}
		}

		/// <summary>
		/// 复制一份word
		/// </summary>
		/// <param name="source"></param>
		/// <param name="dest"></param>
		public void CopyWord(string source,string dest)
		{
			try
			{
				CreateDirectory(dest);
				source = Path.GetFullPath(source);
				dest = Path.GetFullPath(dest);
				File.Copy(source, dest);
			}
			catch (Exception e)
			{
				Console.WriteLine($"复制word出错:{e.ToString()}");
			}
		}

		/// <summary>
		/// 移动word
		/// </summary>
		/// <param name="source"></param>
		/// <param name="dest"></param>
		public void MoveWord(string source, string dest)
		{
			try
			{
				CreateDirectory(dest);
				source = Path.GetFullPath(source);
				dest = Path.GetFullPath(dest);
				File.Move(source, dest);
			}
			catch (Exception e)
			{
				Console.WriteLine($"移动word出错:{e.ToString()}");
			}
		}

		/// <summary>
		/// 是否已经提交周报
		/// </summary>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public bool WordSubmitted(string fileName)
		{
			return File.Exists(fileName);
		}


		//创建文件夹
		private void CreateDirectory(string dest)
		{
			if (Path.HasExtension(dest))
			{
				string directoryPath = dest.Substring(0, dest.LastIndexOf("\\"));
				directoryPath = Path.GetFullPath(directoryPath);
				//如果目标路径不存在 则创建一个
				if (!Directory.Exists(directoryPath))
					Directory.CreateDirectory(directoryPath);
			}
			else
			{
				string directoryPath = Path.GetFullPath(dest);
				//如果目标路径不存在 则创建一个
				if (!Directory.Exists(directoryPath))
					Directory.CreateDirectory(directoryPath);
			}

		}


	}
}